﻿// CheckElementLibraryDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "CheckElementLibraryDialog.h"
#include "afxdialogex.h"

#include "ElementTabView.h"


// CCheckElementLibraryDialog 对话框

IMPLEMENT_DYNAMIC(CCheckElementLibraryDialog, CDialogEx)

CCheckElementLibraryDialog::CCheckElementLibraryDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CHECK_ELEMENT_LIBRARY, pParent)
{
	m_pFrameWnd = NULL;
}

CCheckElementLibraryDialog::~CCheckElementLibraryDialog()
{
}

void CCheckElementLibraryDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_TREE_CHECK_ELEMENT, m_tree);
}


BEGIN_MESSAGE_MAP(CCheckElementLibraryDialog, CDialogEx)
	ON_COMMAND(ID_TOOLBAR_CHECK_ELEMENT_NEW, &CCheckElementLibraryDialog::OnCheckElementNew)
	ON_COMMAND(ID_TOOLBAR_CHECK_ELEMENT_REMOVE, &CCheckElementLibraryDialog::OnCheckElementRemove)
	ON_COMMAND(ID_TOOLBAR_CHECK_SUBELEMENT_NEW, &CCheckElementLibraryDialog::OnCheckSubElementNew)
	ON_COMMAND(ID_TOOLBAR_CHECK_SUBELEMENT_REMOVE, &CCheckElementLibraryDialog::OnCheckSubElementRemove)
	ON_COMMAND(ID_TOOLBAR_CHECK_ITEM_NEW, &CCheckElementLibraryDialog::OnCheckItemNew)
	ON_COMMAND(ID_TOOLBAR_CHECK_ITEM_REMOVE, &CCheckElementLibraryDialog::OnCheckItemRemove)
	ON_WM_CREATE()
	ON_WM_SIZE()
END_MESSAGE_MAP()


// CCheckElementLibraryDialog 消息处理程序


int CCheckElementLibraryDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialogEx::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码

	CString strMyClass = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,
		::LoadCursor(NULL, IDC_ARROW),
		(HBRUSH)::GetStockObject(WHITE_BRUSH),
		::LoadIcon(NULL, IDI_APPLICATION));

	m_pFrameWnd = new CFrameWnd();
	m_pFrameWnd->Create(strMyClass, _T(""), WS_CHILD, CRect(0, 0, 100, 100), this);
	m_pFrameWnd->MoveWindow(0, 0, 500, 400);
	m_pFrameWnd->ShowWindow(SW_SHOW);

	CRect rect;
	GetClientRect(&rect);

	//m_wndSplitter.CreateStatic(m_pwndFrame, 1, 2);
	//m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CElementTreeCtrl), CSize(rect.Width() / 4, rect.Height()), NULL);
	//m_wndSplitter.CreateView(1, 0, RUNTIME_CLASS(CElementListCtrl), CSize(rect.Width() / 4, rect.Height()), NULL);

	//CCreateContext ctxList, ctxTree;
	//ctxTree.m_pNewViewClass = RUNTIME_CLASS(CElementTreeView);
	//ctxTree.m_pCurrentDoc = NULL;
	//ctxTree.m_pNewDocTemplate = NULL;
	//ctxTree.m_pLastView = NULL;
	//ctxTree.m_pCurrentFrame = NULL;

	//ctxList.m_pNewViewClass = RUNTIME_CLASS(CElementListView);
	//ctxList.m_pCurrentDoc = NULL;
	//ctxList.m_pNewDocTemplate = NULL;
	//ctxList.m_pLastView = NULL;
	//ctxList.m_pCurrentFrame = NULL;

	////CRect rect;
	////GetClientRect(&rect);
	//m_wndSplitter.CreateStatic(m_pFrameWnd, 1, 2);
	//m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CElementTreeView), CSize(rect.Width() * 1/4, rect.Height()), &ctxTree);
	//m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CElementListView), CSize(rect.Width() * 3/ 4, rect.Height()), &ctxList);

	CCreateContext ctxList, ctxTree;
	ctxTree.m_pNewViewClass = RUNTIME_CLASS(CElementTreeView);
	ctxTree.m_pCurrentDoc = NULL;
	ctxTree.m_pNewDocTemplate = NULL;
	ctxTree.m_pLastView = NULL;
	ctxTree.m_pCurrentFrame = NULL;

	ctxList.m_pNewViewClass = RUNTIME_CLASS(CElementTabView);
	ctxList.m_pCurrentDoc = NULL;
	ctxList.m_pNewDocTemplate = NULL;
	ctxList.m_pLastView = NULL;
	ctxList.m_pCurrentFrame = NULL;

	//CRect rect;
	//GetClientRect(&rect);
	m_wndSplitter.CreateStatic(m_pFrameWnd, 1, 2);
	m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CElementTreeView), CSize(rect.Width() * 1 / 4, rect.Height()), &ctxTree);
	m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CElementTabView), CSize(rect.Width() * 3 / 4, rect.Height()), &ctxList);


	return 0;
}



BOOL CCheckElementLibraryDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_BORDER | WS_CAPTION | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_TOOLBAR_CHECK_ELEMENT_LIBRARY, 0, 0, TRUE, 0, 0, IDB_PNG_CHECK_ELEMENT_LIBRARY_24))
	{
		TRACE0("未能创建工具栏\n");
		return -1;      // 未能创建
	}

	m_wndToolBar.SetOwner(this);
	m_wndToolBar.SetRouteCommandsViaFrame(FALSE);
	m_wndToolBar.AdjustSizeImmediate();

	//////////////////////////////////////////////////////////////////////////
	AdjustLayout();
	m_pFrameWnd->ShowWindow(SW_SHOW);

	//////////////////////////////////////////////////////////////////////////
	PopulateElementTree();

	//m_tree.LoadImages();
	//m_tree.Init();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}



void CCheckElementLibraryDialog::OnCheckElementNew()
{
	AfxMessageBox("新建检查元素");
}

void CCheckElementLibraryDialog::OnCheckElementRemove()
{
	AfxMessageBox("删除检查元素");
}

void CCheckElementLibraryDialog::OnCheckSubElementNew()
{
	AfxMessageBox("新建检查子元素");
}

void CCheckElementLibraryDialog::OnCheckSubElementRemove()
{
	AfxMessageBox("删除检查子元素");
}

void CCheckElementLibraryDialog::OnCheckItemNew()
{
	AfxMessageBox("新建检查项");
}

void CCheckElementLibraryDialog::OnCheckItemRemove()
{
	AfxMessageBox("删除检查项");
}



void CCheckElementLibraryDialog::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	AdjustLayout();
}


void CCheckElementLibraryDialog::AdjustLayout()
{
	//int margin = GetSystemMetrics(SM_CXFRAME);

	if (m_wndToolBar.GetSafeHwnd())
	{
		CRect tbRect;
		m_wndToolBar.GetWindowRect(&tbRect);
		int nHeight = tbRect.Height();

		if (m_pFrameWnd)
		{
			CRect wndRect;
			GetWindowRect(&wndRect);
			ScreenToClient(wndRect);

			int margin = abs(wndRect.left);

			wndRect.left = margin;
			wndRect.top = nHeight;
			wndRect.right -= (2 * margin);			
			wndRect.bottom -= (2 * margin);

			/*wndRect.left = margin;
			wndRect.right = wndRect - (5* margin);
			wndRect.top = nHeight;
			wndRect.bottom = height - (5*margin + nHeight);*/
			m_pFrameWnd->MoveWindow(wndRect);
		}
	}
}

void CCheckElementLibraryDialog::PopulateElementTree()
{
	CElementTreeView* pTree =  (CElementTreeView*)m_wndSplitter.GetPane(0, 0);
	pTree->Populate();
}

